package com.orange.framework.result;


import com.orange.common.enums.ResultCodeEnum;

import java.io.Serializable;
import java.util.List;

/**
 * ClassName:R
 * Package:com.ywf.myproject.common.result
 * Description:
 *
 * @Date:2021/2/28 11:06
 * @Author:YWF
 */
public class R<T> implements Serializable {

    private static final long serialVersionUID = 8383589797965540076L;

    private String code;

    private String msg;

    private T data;

    private List<Error> errors; // 校验错误信息

    /**
     * 私有构造器
     */
    private R(){}

    /**
     * 通用返回结果
     * @return
     */
    public static R ok(){
        R r = new R();
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMsg(ResultCodeEnum.SUCCESS.getMsg());
        return r;
    }

    /**
     * 通用返回失败，未知错误
     * @return
     */
    public static R error(){
        R r = new R();
        r.setCode(ResultCodeEnum.UNKNOWN_ERROR.getCode());
        r.setMsg(ResultCodeEnum.UNKNOWN_ERROR.getMsg());
        return r;
    }

    /**
     * 设置结果，参数为形参
     * @param result
     * @return
     */
    public static R setResult(ResultCodeEnum result){
        R r = new R();
        r.setCode(result.getCode());
        r.setMsg(result.getMsg());
        return r;
    }

    /**
     * 自定义返回数据
     * @param data
     * @return
     */
    public R data(T data){
        this.setData(data);
        return this;
    }

    /**
     * 自定义返回状态码
     * @param code
     * @return
     */
    public R code(String code){
        this.setCode(code);
        return this;
    }

    /**
     * 自定义返回描述信息
     * @param message
     * @return
     */
    public R message(String message){
        this.setMsg(message);
        return this;
    }

    public R errors(List<Error> errors) {
        this.setErrors(errors);
        return this;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public List<Error> getErrors() {
        return errors;
    }

    public void setErrors(List<Error> errors) {
        this.errors = errors;
    }

    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    public static class Error{
        private String type;
        private String msg;

        public Error(String type, String msg) {
            this.type = type;
            this.msg = msg;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        @Override
        public String toString() {
            return "Error{" +
                    "type='" + type + '\'' +
                    ", msg='" + msg + '\'' +
                    '}';
        }
    }
}
